<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');

/**
 * Init Model
 *
 */


class Sale_model extends CI_Model {

	function __construct(){
		parent::__construct();
	}
	function getStates(){
		$sql = "select * from state where status = 51";
		$result = $this->db->query($sql)->result();
		if($result) return $result;
	}
	function getSale($mem_id)
	{
		$sql = "select * from sale where mem_id = $mem_id";
		$result = $this->db->query($sql)->result();
		if($result) return $result;
	}

	function getSales($status=51,$limit=5)
	{
		$sql = "select * from sale where status = $status limit $limit";
		$result = $this->db->query($sql)->result();
		if($result) return $result;
	}
	
	
	function getsaleInfo($id)
	{
		$sql = "select * from sale where id = $id";
		$result = $this->db->query($sql)->row();
		if($result) return $result;
	}


	function upload($imageData=NULL){

		if ($imageData['imagearray']['error'] == 0) {

			$ext = preg_replace("/.*\.([^.]+)$/", "\\1", $imageData['imagearray']['name']);
			$imagename=strtolower($imageData['imagearray']['name']);
			$imagename=md5(time());
			//die($imagename);

			$imagename=explode( '.',$imagename);
			$imagename=$imagename[0];

			if($imageData['customefilename']){
				$filename=$imageData['customefilename'].'.'.$ext;
			}
			else{
				$filename = $imagename.'.'.$ext;
			}

			if($imageData['namewithtime']=='Yes'||$imageData['namewithtime']=='yes'){
				$filename=$imagename.time().'.'.$ext;
				if($imageData['separator'])
					$filename=$imagename.$imageData['separator'].'.'.$ext;
			}
			//	if($imageData['prefix'])
			//		$filename=$imageData['prefix'].$filename;

			$config['file_name'] = strtolower($filename);
			$config['upload_path'] = $imageData['imagepath'];
			$config['allowed_types'] = $imageData['allowedtype'];
			$config['overwrite'] = $imageData['overwrite'];
			$config['remove_spaces'] = $imageData['removespace'];


			$this->load->library('upload', $config);
			if (!$this->upload->do_upload('userfile')) {

				if($imageData['errmessage'])
					$imagedata=array('message'=>$imageData['errmessage'],'data'=>"" ,'code'=>0);
				else
					$imagedata=array('message'=>$this->upload->display_errors(),'data'=>"",'code'=>0);
				return $imagedata;
			}
			//if sucessfull



			else{
				if($imageData['createthumb']=='Yes'||$imageData['createthumb']=='yes')
				{

					$ext = preg_replace("/.*\.([^.]+)$/", "\\1", $filename);

					$img=explode( '.',$filename);
					$imgfile=$img[0];

					$imgfilename = $imgfile.'_thumb'.'.'.$ext;

					//print $filename;
					//die();


					//$thumb['imgFile'] =
					$thumb['image_library'] = 'gd2';
					$thumb['source_image'] = $imageData['imagepath'].$filename;
					$thumb['new_image'] = $imageData['thumbpath'];
					$thumb['create_thumb'] = true;
					$thumb['maintain_ratio'] = false;
					$thumb['thumb_marker']  = '_thumb' ;
					$thumb['width'] = $imageData['thumbwidth'];
					$thumb['height'] = $imageData['thumbheight'];

					$this->load->library('image_lib', $thumb);
					$this->image_lib->resize();
					if(!$this->image_lib->resize()) echo $this->image_lib->display_errors();
					$this->image_lib->clear();
				}
				if($imageData['sucessmessage'])
					$imagedata=array('message'=>$imageData['sucessmessage'],'data'=>$filename,'code'=>1);
				else
					$imagedata=array('message'=>"Image Uploded Sucesfully...",'data'=>$filename,'code'=>1);
				return $imagedata;
			}
		}
	}
	public function businessImage($member_id) {
		$sql = "select * from sale where id='$member_id' ";
		$query = $this->db->query($sql);
		$row = $query->row();
		return $row;
	}

	function delete($id,$image_file,$fieldName) {
		if($id && $image_file){
			$sql="update sale set $fieldName = 'no-image.jpg' where id=$id";

			$this->db->query($sql);
			//--Delete Image File --//

			$f = BASEPATH.'../assets/images/sale/'.$image_file;
			unlink($f);
		}
	}
	
	function delete_sale($id)
	{
		$this->db->where('id',$id);
    	$this->db->delete('sale');
    	
	}
	
}